// src/main.js
import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import { loadAsyncRoutes } from './router/menu'
import ElementPlus from 'element-plus'
import zhCn from 'element-plus/es/locale/lang/zh-cn'
import 'element-plus/dist/index.css'
import { createPinia } from 'pinia'
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
// 导入 element-plus 图标
import * as ElementPlusIconsVue from '@element-plus/icons-vue'


const app = createApp(App)

// 注册图标组件
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
  app.component(key, component)
}

const pinia = createPinia()
pinia.use(piniaPluginPersistedstate)



async function bootstrap() {
  
  const userStr = localStorage.getItem('user')
  const user = userStr ? JSON.parse(userStr) : null

  if (user && user.role) {
    await loadAsyncRoutes(user.role)
  }

  app.use(ElementPlus, { locale: zhCn })
  app.use(pinia)
  app.use(router)

  app.mount('#app')
}

bootstrap()